//
//  HtmlWebViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/10/7.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "HtmlWebViewController.h"
#import <WebKit/WebKit.h>

@interface HtmlWebViewController () 

@property (strong, nonatomic) WKWebView * webView;
@property (nonatomic,readonly, copy) WKWebViewConfiguration *configuration;

@end

@implementation HtmlWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
//    [self.webView loadHTMLString:self.content baseURL:nil];

    NSURL *url = [NSURL URLWithString:self.url];
    
    // 2. 把URL告诉给服务器,请求,从m.baidu.com请求数据
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 3. 发送请求给服务器
    [self.webView loadRequest:request];
}

-(WKWebView *)webView
{
    if (_webView == nil) {
        // 自适应屏幕宽度js initial-scale=1.0, maximum-scale=1.0, user-scalable=no' 不能缩放
        NSString *jSString = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
        //        NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        // 添加自适应屏幕宽度js调用的方法
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUserScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        
        
        wkWebConfig.preferences = [[WKPreferences alloc] init];
        wkWebConfig.preferences.javaScriptEnabled =YES;
        wkWebConfig.preferences.javaScriptCanOpenWindowsAutomatically =NO;
        
        NSMutableString *javascript = [NSMutableString string];
        [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按
        [javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];//禁止选择
        WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kHomeLineH - kStatusAndNavBarH) configuration:wkWebConfig];
        [_webView.configuration.userContentController addUserScript:noneSelectScript];
        [_webView setAllowsBackForwardNavigationGestures:YES];
        [_webView.configuration.userContentController addUserScript:noneSelectScript];
        _webView.scrollView.bounces = NO;
//        _webView.scrollView.delegate = self;
        _webView.allowsBackForwardNavigationGestures = YES;
    }
    return _webView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 让webview的内容一直居中显示
    scrollView.contentOffset = CGPointMake((scrollView.contentSize.width - WIDTH) / 2, scrollView.contentOffset.y);
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGSize contentSize = webView.scrollView.contentSize;
    CGSize viewSize = self.view.bounds.size;
    float sfactor = viewSize.width / contentSize.width;
    webView.scrollView.minimumZoomScale = sfactor;
    webView.scrollView.maximumZoomScale = sfactor;
    webView.scrollView.zoomScale = sfactor;
}

//-(void)webViewDidFinishLoad:(UIWebView *)webView{
//
//    [webView stringByEvaluatingJavaScriptFromString:
//     @"javascript:var imgs = document.getElementsByTagName(‘img’);"
//     "for(var i = 0; i<imgs.length; i++){"
//     "imgs.style.width = ‘100%';"
//     "imgs.style.height = ‘auto';}"];
//}

////获取webView中的所有图片URL
//- (NSArray *) getImageurlFromHtml:(NSString *) webString
//{
//    NSMutableArray * imageurlArray = [NSMutableArray arrayWithCapacity:1];
//    //标签匹配
//    NSString *parten = @"<img(.*?)>";
//    NSError* error = NULL;
//    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:parten options:0 error:&error];
//    NSArray* match = [reg matchesInString:webString options:0 range:NSMakeRange(0, [webString length] - 1)];
//    for (NSTextCheckingResult * result in match) {
//        //过去数组中的标签
//        NSRange range = [result range];
//        NSString * subString = [webString substringWithRange:range];
//        //从图片中的标签中提取ImageURL
//        NSRegularExpression *subReg = [NSRegularExpression regularExpressionWithPattern:@"http://(.*?)\"" options:0 error:NULL];
//        NSArray* match = [subReg matchesInString:subString options:0 range:NSMakeRange(0, [subString length] - 1)];
//        NSTextCheckingResult * subRes = match[0];
//        NSRange subRange = [subRes range];
//        subRange.length = subRange.length -1;
//        NSString * imagekUrl = [subString substringWithRange:subRange];
//        //将提取出的图片URL添加到图片数组中
//        [imageurlArray addObject:imagekUrl];
//    }
//    return imageurlArray;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
